//提供一个构造函数
function PieChart(option) {
    //初始化操作
    this._init(option)
}
//设置原型对象
PieChart.prototype = {
    //修正构造器属性
    constructor:PieChart,
    //初始化方法
    _init:function (option) {
        option = option || {};
        this.x = option.x || 0;
        this.y = option.y || 0;

        this.innerRadius = option.innerRadius || 0;
        this.outerRadius = option.outerRadius || 0;
        //外圆
        this.outerWidth = option.outerWidth || 0;
        this.outerColor = option.outerColor || 'yellow';
        //数据
        this.dataArray = option.dataArray || [];
        
    },
    render:function (layer) {
        this.group = new Konva.Group();
        layer.add(this.group);
        //创建扇形区
        this.wedgeGroup = new Konva.Group();
        this.group.add(this.wedgeGroup);
        //创建文字区
        this.valueTextGroup = new Konva.Group();
        this.group.add(this.valueTextGroup);
        //创建区域组
        this.areaGroup = new Konva.Group();
        this.group.add(this.areaGroup);

        //创建外圆
        var outerCircle = new Konva.Circle({
            x:this.x,
            y:this.y,
            radius:this.outerRadius,
            stroke: this.outerColor,
            strokeWidth: this.outerWidth
        });
        this.group.add(outerCircle);

        var self = this;
        var startAngle = -90;
        this.dataArray.forEach(function ( item, index) {
            //扇区的角度
            var tempAngle = item.value * 360 ;
            var wedge = new Konva.Wedge({
                x:self.x,
                y:self.y,
                radius:self.innerRadius,
                fill: item.color,
                angle:tempAngle,
                rotation:startAngle
            });
            self.wedgeGroup.add(wedge);

            var text = item.value*100 + "%";
            var textAngle = startAngle + tempAngle*0.5;
            var textX = self.x + (self.outerRadius + 20) * Math.cos(textAngle*Math.PI/180);
            var textY = self.y + (self.outerRadius + 20) * Math.sin(textAngle*Math.PI/180);
            var valueText = new Konva.Text({
                x:textX,
                y:textY,
                text:text,
                fontSize:18,
                fill:item.color
            });
            // 特殊处理
            if(textAngle > 90 && textAngle < 270){
                var newX = textX - valueText.width();
                valueText.x(newX);
            }
            self.valueTextGroup.add(valueText);

            //绘制区域组
            var areaW = 60,areaH = 20;
            var areaX = self.x + self.outerRadius + 80,areaY = self.y + index * (areaH + 10);
            var areaRect = new Konva.Rect({
                x:areaX,
                y:areaY,
                width:areaW,
                height:areaH,
                fill:item.color
            });
            self.areaGroup.add(areaRect);

            var areaText = new Konva.Text({
                x:areaX + areaW + 20,
                y:areaY,
                text: item.name,
                fontSize:20,
                fill:item.color
            });
            self.areaGroup.add(areaText);
            startAngle += tempAngle;
        })
    }
};